#include <NDL.h>
#include <SDL.h>
#include <string.h>
#include <assert.h>

#define keyname(k) #k,

static const char *keyname[] = {
  "NONE",
  _KEYS(keyname)
};

int SDL_PushEvent(SDL_Event *ev) {
	printf("not completed");
	assert(0);
  return 0;
}

size_t get_value_from_key(const char*name,char*buf);
static uint8_t key_state[128] = {};
int SDL_PollEvent(SDL_Event *ev) {
	char buf[128];
	int success = NDL_PollEvent(buf,sizeof(buf));
	if(!success)return 0;
	if(strncmp(buf,"ku:",3)==0){
		ev->type = SDL_KEYUP;ev->key.type = SDL_KEYUP;
		ev->key.keysym.sym = get_value_from_key("keycode",buf);
		key_state[ev->key.keysym.sym] = 0;
 		return 1;
	}
	if(strncmp(buf,"kd:",3)==0){
		ev->type = SDL_KEYDOWN;ev->key.type = SDL_KEYDOWN;
		ev->key.keysym.sym = get_value_from_key("keycode",buf);
		key_state[ev->key.keysym.sym] = 1;
 		return 1;
	}
	return 0;
}

int SDL_WaitEvent(SDL_Event *event) {
	char buf[128];
	while(1){
		if(NDL_PollEvent(buf,sizeof(buf)))break;
	}
	if(strncmp(buf,"ku:",3)==0){event->type = SDL_KEYUP;event->key.type = SDL_KEYUP;}
	if(strncmp(buf,"kd:",3)==0){event->type = SDL_KEYDOWN;event->key.type = SDL_KEYDOWN;}
	event->key.keysym.sym = get_value_from_key("keycode",buf);
 	return 1;
}

int SDL_PeepEvents(SDL_Event *ev, int numevents, int action, uint32_t mask) {
	printf("not completed");
	assert(0);
  return 0;
}
//this function haven't finished yet so the keyboard is not supported yet
uint8_t* SDL_GetKeyState(int *numkeys) {
  return key_state;
}
